package com.yufei.infoExtractor.pfw.entity;

import java.sql.Connection;



import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.yufei.infoExtractor.core.LinkAction;
import com.yufei.infoExtractor.util.sequence.SequenceUUID;




public class Relatedlink extends Entity {
	@Override
	public String toString() {
		return "Relatedlink [seedSiteId=" + seedSiteId + ", link=" + link
				+ ", status=" + status + ", hashCode=" + hashCode + ", depth="
				+ depth + ", upLink=" + upLink + ", findTime=" + findTime
				+ ", title=" + title + ", site=" + site + ", domain=" + domain
				+ ", name=" + name + ", linkType=" + linkType + "]";
	}
	private static Log mLog = LogFactory.getLog(Relatedlink.class);
	private String seedSiteId;
	private String link;
	private Integer status=LinkAction.UNPARSE;
	private String hashCode="";
	private Integer depth;
	private String upLink;
	private Date findTime=null;

	public Date getFindTime() {
		return findTime;
	}

	public void setFindTime(Date findTime) {
		this.findTime = findTime;
	}

	public String getSeedSiteId() {
		return seedSiteId;
	}

	public void setSeedSiteId(String seedSiteId) {
		this.seedSiteId = seedSiteId;
	}

	public String getLink() {
		return link;
	}

	public void setLink(String link) {
		this.link = link;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getHashCode() {
		return hashCode;
	}

	public void setHashCode(String hashCode) {
		this.hashCode = hashCode;
	}

	public Integer getDepth() {
		return depth;
	}

	public void setDepth(Integer depth) {
		this.depth = depth;
	}

	public String getUpLink() {
		return upLink;
	}

	public void setUpLink(String upLink) {
		this.upLink = upLink;
	}
	//关联链接主题(链接文字描述)
    private String title="";
    public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getSite() {
		return site;
	}

	public void setSite(String site) {
		this.site = site;
	}

	public String getDomain() {
		return domain;
	}

	public void setDomain(String domain) {
		this.domain = domain;
	}
	private String site="";
	private String domain="";
	//关联链接标题
	private String name="";
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	private Integer linkType;

	public Integer getLinkType() {
		return linkType;
	}

	public void setLinkType(Integer linkType) {
		this.linkType = linkType;
	}



	/*//detail target links 
	private String relatedLinkId;
	public void setRelatedLinkId(String relatedLinkId) {
		this.relatedLinkId = relatedLinkId;
	}
	private String seedSiteId;
	//关联链接url地址
	private String link;
	//关联链接状态表示该链接是否已经处理，不同类型关联链接是否处理标志也不一样；
	private Integer status=LinkAction.UNPARSE;
	//
	private String hashCode="";
	//关联链接所在深度
	private Integer depth;
	//关联链接父页面URL地址
	private String upLink;
	//关联链接关键字
	private String keyword="";
	
	//关联链接标题
	private String name="";
	//链接类型：表示该链接进一步的处理方式；比如：如果此链接是博客类型链接，则会解析其对应的网页，匹配出其中的博客内容以及评论数等等
	//如果是视音频类型的则会匹配出该关联链接对应的视音频具体内容的链接
	private Integer linkType;
	private String author="";
	private String publicDate="";
	//节目图片链接
	private String imglink="";
	private Integer isFetched=0;
	private String site="";
	private String domain="";
	
	

	
	private Date findTime=new Date();
	public Date getFindTime() {
		return findTime;
	}

	public void setFindTime(Date findTime) {
		this.findTime = findTime;
	}

	public String getDomain() {
		return domain;
	}

	public void setDomain(String domain) {
		this.domain = domain;
	}

	public String getSite() {
		return site;
	}

	public void setSite(String site) {
		this.site = site;
	}

	public Integer getIsFetched() {
		return isFetched;
	}

	public void setIsFetched(Integer isFetched) {
		this.isFetched = isFetched;
	}

	public String getImglink() {
		return imglink;
	}

	public void setImglink(String imglink) {
		this.imglink = imglink;
	}

	public String getAbout() {
		return about;
	}

	public void setAbout(String about) {
		this.about = about;
	}
	//简介
	private String about="";
	public String getPublicDate() {
		return publicDate;
	}

	public void setPublicDate(String publicDate) {
		this.publicDate = publicDate;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}
	//节目浏览数
	private Integer views=0;
	//节目评论数
	private Integer remarks=0;
	//节目具体内容链接（比如：视频类节目的具体内容链接是视频，以视频格式结尾）
	private String contentLink="";
   
	// Constructors

	public Integer getViews() {
		return views;
	}

	public void setViews(Integer views) {
		this.views = views;
	}

	public Integer getRemarks() {
		return remarks;
	}

	public void setRemarks(Integer remarks) {
		this.remarks = remarks;
	}

	public String getContentLink() {
		return contentLink;
	}

	public void setContentLink(String contentLink) {
		this.contentLink = contentLink;
	}

	*//** default constructor *//*
	public Relatedlink() {
		super();
	}
	public void setLinkType(Integer linkType) {
		this.linkType = linkType;
	}
	public String getRelatedLinkId() {
		return relatedLinkId;
	}

	
	public String getSeedSiteId() {
		return seedSiteId;
	}

	public void setSeedSiteId(String seedSiteId) {
		this.seedSiteId = seedSiteId;
	}

	public String getLink() {
		return link;
	}

	public void setLink(String link) {
		this.link = link;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getHashCode() {
		return hashCode;
	}

	public void setHashCode(String hashCode) {
		this.hashCode = hashCode;
	}

	public Integer getDepth() {
		return depth;
	}

	public void setDepth(Integer depth) {
		this.depth = depth;
	}

	public String getUpLink() {
		return upLink;
	}

	public void setUpLink(String upLink) {
		this.upLink = upLink;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	
//判断关联链接重复标准，如果两个关联链接的hashCode一样的话则Set集合只会接受其中一个
	//这里判断的标准是link的hashcode是否一样，因为考虑到两个关联链接URL地址一样但是出现在不同深度页面里面或者是出现的页面不一样又或者其他的信息不一样
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
	//	result = prime * result + ((depth == null) ? 0 : depth.hashCode());
	//	result = prime * result
		//		+ ((hashCode == null) ? 0 : hashCode.hashCode());
	//	result = prime * result + ((keyword == null) ? 0 : keyword.hashCode());
		result = prime * result + ((link == null) ? 0 : link.hashCode());
		result = prime * result
				+ ((linkType == null) ? 0 : linkType.hashCode());
	//	result = prime * result + ((name == null) ? 0 : name.hashCode());
	//	result = prime * result
	//			+ ((relatedLinkId == null) ? 0 : relatedLinkId.hashCode());
	//	result = prime * result
	//			+ ((seedSiteId == null) ? 0 : seedSiteId.hashCode());
	//	result = prime * result + ((status == null) ? 0 : status.hashCode());
	//	result = prime * result + ((title == null) ? 0 : title.hashCode());
	//	result = prime * result + ((upLink == null) ? 0 : upLink.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if(link.equals(((Relatedlink)obj).getLink())){
			return true;
		}
		return false;
	}
	//relatedlinkid,seedsiteid,link,status,hashcode,depth,uplink,keyword,title,name,linktype,views,remarks,contentlink
public  static PreparedStatement getPsBatchInsert(Map<String,Relatedlink> links,Connection conn){  
	PreparedStatement ps=null;
	Iterator it=links.values().iterator();
	Relatedlink link=null;
	int linkCountNumber = 1;
   
	try {
		ps=conn.prepareStatement(AppConstants.relatedlinkinsert);
	} catch (SQLException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	while(it.hasNext()){
		link=(Relatedlink) it.next();
		link.setRelatedLinkId(SequenceUUID.getInstance().getUUID());
		mLog.info("Link "+linkCountNumber + " url:" + (link.getLink()) + " linkType:"+link.linkType + " getDept:" + link.getDepth());
		//排除一些不需要保存的连接类型
		if(link.getLinkType()==LinkAction.LINKTYPE_LINKS||link.getLinkType()==LinkAction.LINKTYPE_UNKNOW){
			continue;
			}
	
		
		if(link.getDepth()==null){link.setDepth(0);
		}
		
		
		
		try {
			
		ps.setString(1, link.getRelatedLinkId());
		
		ps.setString(2, link.getSeedSiteId());
		ps.setString(3, link.getLink());
		ps.setInt(4, link.getStatus());
		//关联链接指纹
		ps.setString(5,link.getHashCode());
		ps.setInt(6, link.getDepth());
		ps.setString(7, link.getUpLink());
		ps.setString(8, link.getKeyword());
		ps.setString(9, link.getTitle());
		ps.setString(10, link.getName());
		ps.setInt(11, link.getLinkType());
		ps.setInt(12, link.getViews());
		ps.setInt(13,link.getRemarks());
		ps.setString(14, link.getContentLink());
		ps.setString(15, link.getAuthor());
		ps.setString(16, link.getPublicDate());
		ps.setString(17, link.getAbout());
		ps.setString(18, link.getImglink());
		ps.setString(19, link.getSite());
		ps.setString(20, link.getDomain());
		//java.sql.Date 是针对SQL语句使用的，它只包含日期而没有时间部分
		//ps.setDate(21, new java.sql.Date(link.getFindTime().getTime()));
		ps.setTimestamp(21, new Timestamp(System.currentTimeMillis()));
		ps.addBatch();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	return ps;
}

public Integer getLinkType() {
	return linkType;
}*/

	

}